package com.watson.juc;

import java.util.concurrent.TimeUnit;

/**
 * @author watson
 * @date 2021/8/2 10:58
 * @Description: TODO
 **/
public class JMMDemo {

    // 添加volatile后，主线程改num的值后，线程1立马跳出循环
    public volatile static int num = 0;

    public static void main(String[] args) {//主线程
        new Thread(() -> { //线程1
            while (num == 0) {
                //
            }
        }).start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //改变值，希望线程1 ，不再执行，结束循环
        num = 1;
        System.out.println(num);
    }
}
